home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_exim.idb / usr / freeware / bin / exicyclog.z / exicyclog
Encoding:
Text File  |  1999-01-26  |  6.1 KB  |  176 lines

  1. #! /bin/sh
  2.  
  3. # Copyright (c) 1998 University of Cambridge.
  4. # See the file NOTICE for conditions of use and distribution.
  5.  
  6.  
  7. # Shell script for cycling exim main and reject log files. Each time it is run,
  8. # the files get "shuffled down" by one, the current one (e.g. mainlog) becoming
  9. # mainlog.01, the previous mainlog.01 becoming mainlog.02, and so on, up to the
  10. # limit configured here. The same happens to the reject logs. All those with
  11. # numbers greater than 1 are compressed.
  12.  
  13. # This script should be called regularly (e.g. daily) by a root crontab
  14. # entry of the form
  15.  
  16. # 1 0 * * *   /opt/exim/bin/exicyclog
  17.  
  18. # The following lines are generated from Exim's configuration file when
  19. # this source is built into a script, but you can subsequently edit them
  20. # without rebuilding things, as long are you are careful not to overwrite
  21. # the script in the next Exim rebuild/install. "Keep" is the number of old log
  22. # files that are required to be kept. "Compress" and "suffix" define your
  23. # chosen compression method. The others are provided because the location
  24. # of certain commands varies from OS to OS. Sigh.
  25.  
  26. keep=10
  27. compress=/usr/sbin/gzip
  28. suffix=gz
  29.  
  30. chown=/usr/bin/chown
  31. chgrp=/usr/bin/chgrp
  32. mv=/bin/mv
  33. rm=/bin/rm
  34.  
  35. # End of editable lines
  36.  
  37.  
  38. # Determine if the log file path is set, and where the spool directory is. The
  39. # strings /usr/freeware/lib/exim/configure and /usr/freeware/bin below are replaced by their compile-
  40. # time settings when this source is build into a script. Search for an
  41. # exim_path setting in the configure file; otherwise use the bin directory.
  42. # Call that version of Exim to find the spool directory and log file path.
  43.  
  44. exim_path=`grep '^[     ]*exim_path' /usr/freeware/lib/exim/configure | sed 's/.*=[     ]*//'`
  45. if test "$exim_path" = ""; then exim_path=/usr/freeware/bin/exim; fi
  46.  
  47. spool_directory=`$exim_path -C /usr/freeware/lib/exim/configure -bP spool_directory | sed 's/.*=[  ]*//'`
  48. log_file_path=`$exim_path -C /usr/freeware/lib/exim/configure -bP log_file_path | sed 's/.*=[  ]*//'`
  49.  
  50. # If log_file_path is empty, then the logs we are interested in are called
  51. # "mainlog" and "rejectlog" in the directory called "log" in the spool
  52. # directory. Otherwise we fish out the directory from the given path, and
  53. # also the names of the logs.
  54.  
  55. if [ "$log_file_path" = "" ]; then
  56.   logdir=$spool_directory/log
  57.   mainlog=mainlog
  58.   rejectlog=rejectlog
  59. else
  60.   logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
  61.   logbase=`echo $log_file_path | sed 's?^.*/??'`
  62.   mainlog=`echo $logbase | sed 's/%s/main/'`
  63.   rejectlog=`echo $logbase | sed 's/%s/reject/'`
  64. fi
  65.  
  66. # Get into the log directory to do the business.
  67.  
  68. cd $logdir
  69.  
  70. # If there is no main log file, do nothing.
  71.  
  72. if [ ! -f $mainlog ]; then exit; fi
  73.  
  74. # Find out the owner and group of the main log file so that we can re-instate
  75. # this on moved and compressed files, since some operating systems may change
  76. # things. This is a tedious bit of code, but it should work both in operating
  77. # systems where the -l option of ls gives the user and group, and those in which
  78. # you need -lg. The condition is that, if the fifth field of the output from
  79. # ls consists entirely of digits, then the third and fourth fields are the user
  80. # and group.
  81.  
  82. a=`ls -lg $mainlog`
  83. b=`ls -l  $mainlog`
  84.  
  85. # These statements work fine in the Bourne or Korn shells, but not in Bash.
  86. # So for the benefit of systems whose /bin/sh is really Bash, they have been
  87. # changed to a messier form.
  88.  
  89. # user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
  90. # group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`
  91.  
  92. user=`echo "$a
  93. $b
  94. " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $3; exit; } }'`
  95.  
  96. group=`echo "$a
  97. $b
  98. " | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) { print $4; exit; } }'`
  99.  
  100. # Now do the job. Originally the files were numbered 1, 2, etc., but it was
  101. # pointed out that using 01, 02, etc. would be better. So that things continue
  102. # to work, we first of all rename any files we find that have old names. This
  103. # code was written in February 1998. It can be removed at some point in the
  104. # future. Say around the millennium. To save running this pointlessly again
  105. # and again, predicate it on the existence of mainlog.1.
  106.  
  107. if [ -f $mainlog.1 ]; then
  108.   count=9
  109.   while [ $count -gt 0 ]; do
  110.     if [ -f $mainlog.$count ]; then
  111.       $mv $mainlog.$count $mainlog.0$count;
  112.     fi;
  113.     if [ -f $mainlog.$count.$suffix ]; then
  114.       $mv $mainlog.$count.$suffix $mainlog.0$count.$suffix;
  115.     fi;
  116.     if [ -f $rejectlog.$count ]; then
  117.       $mv $rejectlog.$count $rejectlog.0$count;
  118.     fi;
  119.     if [ -f $rejectlog.$count.$suffix ]; then
  120.       $mv $rejectlog.$count.$suffix $rejectlog.0$count.$suffix;
  121.     fi;
  122.     count=`expr $count - 1`
  123.   done
  124. fi
  125.  
  126. # Now operate on the new style of name only.
  127.  
  128. if [ $keep -lt 10 ]; then keept=0$keep; else keept=$keep; fi;
  129.  
  130. if [ -f $mainlog.$keept ]; then $rm $mainlog.$keept; fi;
  131. if [ -f $mainlog.$keept.$suffix ]; then $rm $mainlog.$keept.$suffix; fi;
  132.  
  133. if [ -f $rejectlog.$keept ]; then $rm $rejectlog.$keept; fi;
  134. if [ -f $rejectlog.$keept.$suffix ]; then $rm $rejectlog.$keept.$suffix; fi;
  135.  
  136. while [ $keep -gt 1 ]; do
  137.   old=`expr $keep - 1`
  138.   if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
  139.   if [ -f $mainlog.$oldt ]; then
  140.     $mv $mainlog.$oldt $mainlog.$keept
  141.     $compress $mainlog.$keept
  142.     $chown $user $mainlog.$keept.$suffix
  143.     $chgrp $group $mainlog.$keept.$suffix
  144.   elif [ -f $mainlog.$oldt.$suffix ]; then
  145.     $mv $mainlog.$oldt.$suffix $mainlog.$keept.$suffix
  146.     $chown $user $mainlog.$keept.$suffix
  147.     $chgrp $group $mainlog.$keept.$suffix
  148.   fi
  149.   if [ -f $rejectlog.$oldt ]; then
  150.     $mv $rejectlog.$oldt $rejectlog.$keept
  151.     $compress $rejectlog.$keept
  152.     $chown $user $rejectlog.$keept.$suffix
  153.     $chgrp $group $rejectlog.$keept.$suffix
  154.   elif [ -f $rejectlog.$oldt.$suffix ]; then
  155.     $mv $rejectlog.$oldt.$suffix $rejectlog.$keept.$suffix
  156.     $chown $user $rejectlog.$keept.$suffix
  157.     $chgrp $group $rejectlog.$keept.$suffix
  158.   fi
  159.   keep=$old
  160.   keept=$oldt
  161. done
  162.  
  163. if [ -f $mainlog ]; then
  164.   $mv $mainlog $mainlog.01
  165.   $chown $user $mainlog.01
  166.   $chgrp $group $mainlog.01
  167. fi
  168.  
  169. if [ -f $rejectlog ]; then
  170.   $mv $rejectlog $rejectlog.01
  171.   $chown $user $rejectlog.01
  172.   $chgrp $group $rejectlog.01
  173. fi
  174.  
  175. # End of exicyclog
  176.